package com.huarui.part.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.struts2.ServletActionContext;
import org.ewaf.framework.action.BaseAction;

import com.huarui.part.model.PartUpload;
import com.huarui.part.service.PartUploadService;
import com.huarui.part.util.PartOperateResultEnum;
import com.huarui.user.model.SessionMessage;
import com.huarui.user.security.UserHolder;
import com.opensymphony.xwork2.Action;

public class PartUploadExeclAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 3690407478354937930L;

	public String fileName;

	private File excel;

	private static final Logger logger = Logger
			.getLogger(PartUploadAddAction.class);

	private PartUploadService partUploadService;

	private Map<String, Object> resultMap;

	public InputStream getDownloadFile() {
		return ServletActionContext.getServletContext().getResourceAsStream(
				"/htdocs/html/" + fileName);
	}

	public String getDownloadChineseFileName() {

		return fileName;
	}

	public String getFileName() {
		return fileName;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}

	public String down() {
		return Action.SUCCESS;
	}

	public String readExcelData() throws IOException {

		boolean hasError = false;
		POIFSFileSystem fs;
		SessionMessage sm = UserHolder.getCurrentSessionMessage();

		try {
			List<PartUpload> list = new ArrayList<PartUpload>();
			fs = new POIFSFileSystem(new FileInputStream(excel));
			HSSFWorkbook wb = new HSSFWorkbook(fs);

			HSSFSheet sheet = wb.getSheetAt(0);
			int k=0;
			for (int j = 1; j <= sheet.getLastRowNum(); j++) {
				HSSFRow row = sheet.getRow(j);

				PartUpload partUpload = new PartUpload();
				partUpload.setCompanyId(sm.getCompanyId());
				partUpload.setCompanyName(sm.getCompanyName());
				
				try {
					for ( k = 0; k < 6; k++) {
						HSSFCell cell = row.getCell(k);
						if (cell == null)
							continue;

						if (k == 0) {
							partUpload
									.setCode(cell.getStringCellValue().trim());
						}
						if (k == 1) {
							partUpload
									.setName(cell.getStringCellValue().trim());
						}
						if (k == 2) {
							partUpload.setNameEn(cell.getStringCellValue()
									.trim());
						}
						if (k == 3) {
							partUpload.setMeasurementUnit(cell
									.getStringCellValue().trim());
						}
						if (k == 4) {
							partUpload.setBrand(cell.getStringCellValue()
									.trim());
						}
						if (k == 5) {
							partUpload.setCarModel(cell.getStringCellValue()
									.trim());
						}

					}
					list.add(partUpload);
				} catch (Exception e) {
					hasError = true;
					resultMap = new HashMap<String, Object>();
					resultMap.put(PartOperateResultEnum.SUCCESS.getMsg(), true);
					resultMap.put("msg", "第"+ j+"行第"+(k+1)+"例格式错误");
					break;
				}

			}
			if (hasError == false) {
				resultMap = partUploadService.save(list);
			}
		} catch (FileNotFoundException e) {

			logger.error("零件编码上传excel出错", e);
		} catch (IOException e) {
			logger.error("零件编码上传excel出错", e);
		}

		return Action.SUCCESS;
	}

	public File getExcel() {
		return excel;
	}

	public void setExcel(File excel) {
		this.excel = excel;
	}

	public Map<String, Object> getResultMap() {
		return resultMap;
	}

	public void setResultMap(Map<String, Object> resultMap) {
		this.resultMap = resultMap;
	}

	public void setPartUploadService(PartUploadService partUploadService) {
		this.partUploadService = partUploadService;
	}

}
